package com.lebang.controller.api.earned;

import com.lebang.common.Require;
import com.lebang.common.ValidCode;
import com.lebang.entity.RequestHeaders;
import com.lebang.model.RequestUserInfo;
import com.lebang.model.back.Result;
import com.lebang.model.enums.buss.TaskCategory;
import com.lebang.service.MemberNewTaskService;
import com.lebang.service.MemberNoticeService;
import com.lebang.service.PointService;
import com.lebang.feign.EatEarnedFeign;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 吃饭赚
 *
 * @Author: hwq
 * @Date: 2020/1/8 13:52
 * @Version 1.0
 */
@Api(tags = "吃饭赚")
@RestController
@RequestMapping("/app/ear/eat")
public class EatEarnedApi {
    @Autowired
    private EatEarnedFeign eatEarnedFeign;
    @Autowired
    private PointService pointService;
    @Autowired
    private MemberNewTaskService memberNewTaskService;
    @Autowired
    private MemberNoticeService memberNoticeService;


    /**
     * 获取金币列表 -- app
     *
     * @return 返回结果
     */
    @Require
    @ApiOperation("获取金币列表")
    @PostMapping("/list")
    public JsonData list(HttpServletRequest request) {
        RequestUserInfo userInfo = new RequestUserInfo(request);
        return eatEarnedFeign.list(userInfo);
    }

    /**
     * 领取吃饭补贴 -- app
     *
     * @param request 请求数据
     * @return 返回结果
     */
    @Require
    @ApiOperation("领取吃饭补贴")
    @PostMapping("/have")
    @ValidCode
    public JsonData have(HttpServletRequest request) {
        RequestUserInfo userInfo = new RequestUserInfo(request);
        return eatEarnedFeign.have(userInfo);
    }

    /**
     * 点击领取金币奖励 -- app
     *
     * @param request 请求数据
     * @return 返回结果
     */
    @Require
    @ApiOperation("点击领取金币奖励")
    @PostMapping("/reward")
    @ValidCode
    public JsonData reward(HttpServletRequest request, Integer id) {
        RequestUserInfo userInfo = new RequestUserInfo(request);
        RequestHeaders headers = new RequestHeaders(request);
        Result<Integer> result = eatEarnedFeign.reward(userInfo, id);
        if (result.isSuccess()) {
            pointService.addPoint(userInfo.getMemberId(), result.getData(),
                    TaskCategory.EAT_EARNS, headers, "");
            memberNoticeService.addNoticeAndPush("吃饭赚", "完成吃饭赚阶段奖励" + result.getData() + "金币", headers.getUserid(), headers);
            memberNewTaskService.addRecord(6001, userInfo.getMemberId());
            return Callback.success(result.getStatus(), result.getMessage(), result.getData());
        } else {
            return Callback.fail(result.getStatus(), result.getMessage());
        }

    }
}
